package com.itheima.tilas.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectResult;
import com.itheima.tilas.Utils.OssUtil;
import com.itheima.tilas.Utils.ResultUtil;
import com.itheima.tilas.config.AliyunOssConfig;
import com.itheima.tilas.config.OssClient;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Objects;
import java.util.UUID;

/**
 * 文件上传接口
 *
 * @author 132
 */
@Slf4j
@RestController
@RequestMapping("/upload")
public class FailController {
    @Resource
    OssClient ossConfig;

    /**
     * MultipartFile Spring封装的用于接收文件的对象
     *
     * @param file 接收文件的变量名
     * @return 返回
     */
    @PostMapping
    public ResultUtil upload(MultipartFile file) throws IOException {

        //获取参数名
//        LTAI5tMvU4SRtW3sM8wHaL7P             id
//        vIt8nujvBxWyi7OlGYI0u3HAFOZw9t      secret
        log.info("name{}", file.getName());
        //获取实际上传文件名
        log.info("originalName{}", file.getOriginalFilename());
        String url = ossConfig.upload(file.getName(), file.getInputStream());
        if (Objects.isNull(url)) {
            return ResultUtil.fail("上传失败");
        } else {
            return ResultUtil.success("路径", url);
        }

    }
}


//        try {
////转移文件
//String path = "D:/" + uuid + suffix;
//            file.transferTo(new File(path));
//        return ResultUtil.success(path, path);
//        } catch (IOException e) {
//        throw new RuntimeException(e);
//        }